#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2021/7/19 11:28
# @Author  : Samge
import sys

from pyqt5_plugins.examplebutton import QtWidgets

from itkz.controls.itkz_control import ItkzControl
from itkz.resources.custom_view.BaseTableWidget import BaseTableWidget
from itkz.spider_ext.pipelines.itkz import ItkzDb
from itkz.resources.itkz import Ui_Form


class ItkzPanel(BaseTableWidget, Ui_Form):

    BT_TEXT_CRAWL_LIST = "抓取列表"
    BT_TEXT_CRAWL_BD = "抓取网盘"

    # 表查询字段名的配置
    fields_key = {
        'table_name': 'ItkzData',
        'field_status': 'i_status',
        'field_dataId': 'i_dataId',
        'field_title': 'i_title',
        'field_order_by': 'i_id',
        'field_keyword': '',
        'field_query_fields': '',
        'field_other_params': '',
        'field_group_by': ''
    }

    def __init__(self):
        super().__init__()
        self.filter_input.IS_MYSQL = True
        self.control = ItkzControl(self)
        self.control.init_config_value()
        self.filter_input.init_config_value()

    def setup_db(self):
        self.itkz_db = self.itkz_db or ItkzDb()

    def get_results(self, custom_sql):
        self.tv_log.setVisible(False)
        self.tableWidget.setVisible(True)
        return self.itkz_db.query(custom_sql)

    def save_data_list(self, temp_list):
        return self.update_data_to_db(temp_list, self.itkz_db)

    def get_token(self):
        self.control.get_token()

    def crawl_list(self):
        self.control.parse_spider(self.bt_crawl_list, self.BT_TEXT_CRAWL_LIST)

    def crawl_baiduwangpan(self):
        self.control.parse_spider(self.bt_crawl_bd, self.BT_TEXT_CRAWL_BD)

    def parse_baiduwangpan(self):
        self.control.parse_baiduwangpan()


def main():
    app = QtWidgets.QApplication(sys.argv)
    gui = ItkzPanel()
    gui.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
